import { AnyKeyObject } from '../interfaces/index'
import {isDataType} from './is'
export function observeTemplate(template: AnyKeyObject, onChange: Function, watchKeys: string[]) {
  watchKeys.forEach(key => {
    if (!template.hasOwnProperty(key)) return false;
    let value = template[key]
    if (isDataType(value, 'Object')) {
      observeTemplate(value, onChange, watchKeys);
    } else {
      Object.defineProperty(template, key, {
        configurable: true,
        get() {
          return value;
        },
        set(val) {
          if (value === val) return false;
          value = val;
          if (watchKeys.includes(key)) {
            onChange && onChange(key, template);
          }
        }
      })
    }
  })
}
